import { userLogin} from "@/api/user"
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token:getToken(),
    msg:""
  },

  mutations: {
    userLogin:(state,msg)=>{
      state.msg=msg.message;
    },
    Set_token:(state,token)=>{
        state.token=token;
        state.msg=token;
    }
  },

  actions: {
    //登录
    Login({commit},playload){
    //   console.log(playload);
      return new Promise((resolve, reject) => {
        userLogin(playload).then(response => {
          setToken(response.token)
          commit("userLogin",response);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    LogOut({commit}){
        return new Promise(resolve => {
            commit('Set_token', '')
            removeToken()
            resolve()
          })
    }
  }

}

export default user;
